package com.le.tester.compared;

import java.util.concurrent.atomic.LongAdder;

/**
 * createTime：2021/11/18 10:21
 * description：LongAdderTest,依然使用(i++),即使使用了LongAdder,还是继续使用了volatile
 */
public class LongAdderTest {
    private static final int THREAD_COUNT = 20;

    //默认初始化的值为0
    private static volatile LongAdder race = new LongAdder();

    public static void increment() {
        //直接替代了i++
        race.increment();
    }

    public static void main(String[] args) {
        Thread[] threads = new Thread[THREAD_COUNT];
        for (int i = 0; i < THREAD_COUNT; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    increment();
                }
            });
            threads[i].start();
        }
        while (Thread.activeCount() > 1) {
            Thread.yield();
        }
        System.out.println("race:" + race);
    }
}
